// pages/personal/sale/my-team.js
const app = getApp()
var mtabW;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        current: 0,
        currentActive: 0,
        slideOffset: 0,
        tabW: 0,
        tabs: [{
                name: '一级',
                amount: 0
            },
            {
                name: '二级',
                amount: 0
            },
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        wx.getSystemInfo({
            success: function(res) {
                mtabW = res.windowWidth / 2; //设置tab的宽度
                that.setData({
                    tabW: mtabW
                })
            }
        });
        app.http.get('city/tiny-station/get-count?type=1').then(res => {
            this.setData(res.data)
            if (this.data.current == 0) {
                this.data.tabs[0].amount = res.data.top || 0
                this.data.tabs[1].amount = res.data.second || 0
            }
            if (this.data.current == 1) {
                this.data.tabs[1].amount = res.data.second || 0
            }
            this.setData({
                tabs
            })
        })
        this.loadData()
    },
    navClick(e) {
        const {
            current
        } = e.currentTarget.dataset
        var offsetW = e.currentTarget.offsetLeft;
        console.log(e, current)
        this.setData({
            current: current,
            slideOffset: offsetW
        })
        this.loadData()
    },
    loadData() {
        const {
            current,
            tabs
        } = this.data
        let {
            page = 0, list = [], loading = false, nextFlag = true
        } = tabs[current]
        if (loading || !nextFlag) {
            return
        }
        wx.showLoading({
            title: '加载中...',
        })
        this.setData({
            [`tabs[${current}].loading`]: true
        })
        let url = ''
        if (this.data.current == 0) {
            url = 'city/tiny-station/get-top-order'
        }
        if (this.data.current == 1) {
            url = 'city/tiny-station/get-second-order'
        }
        console.log(page, 'tabs[this.data.current].page')

        app.http.get(url, {
            data: {
                page: page
            }
        }).then(res => {
            this.setData({
                tabs
            })
            page++
            nextFlag = res.data.nextFlag
            if (res.data.partners) {
                list.push(...res.data.partners)
            }
            loading = false
            this.setData({
                [`tabs[${current}]`]: Object.assign(tabs[current], {
                    page,
                    nextFlag,
                    list,
                    loading
                }),
            })
            wx.hideLoading()
        })
    },
    onReachBottom: function() {
        this.loadData()
    },

})